GBML manual / Core

event_call

Runs an event right now. event_call(user0) runs this instance’s own User 0 event (n is 0-15); event_call(obj_name.user0) runs EVERY instance of obj_name’s; event_call(someRef.user0) runs ONLY the one instance someRef currently points at; event_call(parent) runs the parent’s version of the event you are in, then carries on with your own code.

Works differently on Game Boy - User events work on the Game Boy, with one limit: a User event cannot call a User event with its own number, not even one of another object, so give the events that call each other different numbers (the build stops and says so).

Works differently on Game Boy Color - User events work on the Game Boy Color, with one limit: a User event cannot call a User event with its own number, not even one of another object, so give the events that call each other different numbers (the build stops and says so).

On other consoles this differs: see Side by side and How the consoles differ.

Syntax

event_call(event)

Arguments

NameType
eventuser_event

Returns

Nothing.

Description

Runs an event right now, as though it had fired on its own. Four forms. The first three run a User n event (n is 0-15) and are told apart by what (if anything) is named before the dot; the fourth, event_call(parent), runs your parent's version of the event you are in (it has a section of its own, further down):

If you want "which one instance", reach for a ref<obj> (see instance_nearest) and the third form - not a hand-rolled id, which this engine does not have (see the id variable's own page for why).

user0-user15 are reserved words: a variable can never be named one, in any position.

Example

event_call(user0);

Runs this instance's own User 0 event right now, from inside Step.

event_call(obj_doc_target.user0);

Runs User 0 on every instance of obj_doc_target currently alive - never this instance's own, unless this instance is itself an obj_doc_target.

var target = instance_nearest(x, y, obj_doc_target);
event_call(target.user0);

Runs User 0 on just the one obj_doc_target instance target points at - never every obj_doc_target, and never a crash if target turns out to be nothing or that instance is already gone (nothing runs).

The parent's version

event_call(parent) runs your parent's version of the event you are inside. Then your own code carries on after the call.

A child object inherits any event it does not write. But once a child writes an event, that version replaces the parent's, even when the child's version is empty or holds only a comment. event_call(parent) is how you keep the parent's version and add your own. Put the call on the line where you want the parent's code to run.

It always runs the same event you are in. Inside Create it runs the parent's Create. Inside Step it runs the parent's Step. parent is the only thing you can put in the brackets for this form; it is not a variable, so a variable of your own called parent is left alone.

Objects and parents explains parents, children and this replace rule from the start.

When there is nothing to run

If there is no parent version to run, the call does nothing. It is not an error and it gives no warning:

The call goes to the nearest ancestor that writes the event, the same rule an inherited event follows. If the parent does not write it but the grandparent does, the grandparent's version runs.

Where it works

Directly inside an event of an object. Not inside a script, because a script is shared by every kind of object and is not an event, so there is no parent version of anything to run. Not inside a with block, because there self is a different instance. Both are compile errors.

Examples

event_call(parent);
shield = 5;

In a child's Create. The parent's Create runs first, then shield is set. The child ends up with the parent's variables and its own.

hp += 1;
event_call(parent);

In a child's Step. The child's own line runs first, then the parent's Step. The order is the order you write the call in.

event_call(parent);
weight = 5;

In an object with no parent. The call does nothing, and weight is set as normal.

The old name

This used to be written run_parent_event(). That name is retired: the check stops with a message that says to write event_call(parent).

On the GBA

The explicit obj_name.userN form is not "a held reference’s event" - obj_name must be a literal project object name, resolved at compile time exactly like instance_create’s own object argument, and it reaches every LIVE instance of that type via the same gbml::pool_of<T>().for_each(...) a with-block already uses, never one specific instance. The someRef.userN form is the opposite: someRef must be a real ref<T> VALUE (from instance_nearest and friends, a declared ref<T> variable/field, ...) - never the bare self/other keyword, which types as ref<T> too but is not one at the C++ level - and it is generation-checked exactly like instance_exists(): a stale or destroyed reference silently runs nothing, never a crash and never the wrong instance. user0..user15 are reserved words - a variable can never be named one, in any position. event_call(parent) does nothing if there is no parent version to run. The GBA does no searching for it while the game runs: the build picks the parent’s function ahead of time, so the call costs about as much as any other function call. It goes to the nearest ancestor that writes this event itself: if the parent does not write it but the grandparent does, the grandparent’s version runs. If there is no parent, or no ancestor writes this event, the call compiles to nothing - no error and no warning. It only works directly inside an object’s own event: not in a script, and not inside a with block (there, self is a different instance). The old name run_parent_event() is retired; the check says to write event_call(parent).

See also: instance_nearestObjects and parentsObjects and instances